package com.example.eeuse.controller;


import com.example.eeuse.mapper.es.DocumentMapper;
import com.example.eeuse.pojo.es.Document;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 测试使用Easy-ES
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestUseEeController {
    private final DocumentMapper documentMapper;

    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        return documentMapper.createIndex();
    }

    @GetMapping("/insert")
    public Integer insert() {
        // 2.初始化-> 新增数据
        int result = 0;
        for (int i = 0; i < 100; i++) {
            Document document = new Document();
            document.setTitle("老汉"+i);
            document.setContent("推*技术过硬"+i);
            result += documentMapper.insert(document);
        }
        return result;
    }

    @GetMapping("/search")
    public EsPageInfo<Document> search(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        // wrapper.eq(Document::getTitle, "老汉");

        return documentMapper.pageQuery(wrapper, pageNum, pageSize);
    }
}
